﻿using System;
using System.Linq;

namespace BSPortal.Usercontrol.Admin.Book.Order
{
    public partial class frmOrder : System.Web.UI.Page
    {
        private short id = 0;
        private MainService service = new MainService();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadCity();
                LoadDistrict();
                LoadWard();

                LoadData();
            }
        }

        protected void LoadCity()
        {
            var mainService = Common.GetClientService();
            var listCity = mainService.GetAllCity().OrderBy(row => row.Name);

            ddlCity.DataValueField = "CityId";
            ddlCity.DataTextField = "Name";
            ddlCity.DataSource = listCity;
            ddlCity.DataBind();            
        }

        protected void LoadDistrict()
        {
            if (ddlCity.SelectedValue != string.Empty)
            {
                var mainService = Common.GetClientService();
                var listDistrict = mainService.GetAllDistrict(int.Parse(ddlCity.SelectedValue)).OrderBy(row => row.Name);

                ddlDistrict.DataValueField = "DistrictId";
                ddlDistrict.DataTextField = "Name";
                ddlDistrict.DataSource = listDistrict;
                ddlDistrict.DataBind();
            }
        }

        protected void LoadWard()
        {
            if (ddlDistrict.SelectedValue != string.Empty)
            {
                var mainService = Common.GetClientService();
                var listWard =
                    mainService.GetAllWard(int.Parse(ddlCity.SelectedValue), int.Parse(ddlDistrict.SelectedValue)).OrderBy(row => row.Name);

                ddlWard.DataValueField = "WardId";
                ddlWard.DataTextField = "Name";
                ddlWard.DataSource = listWard;
                ddlWard.DataBind();
            }
        }

        protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDistrict();
            ddlDistrict_SelectedIndexChanged(sender,e);
        }

        protected void ddlDistrict_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadWard();
        }

        void LoadData()
        {
            string str = Request["OrderId"];
            if (str == null)
            {
                
                
            }
            else
            {
                id = short.Parse(str);
                var data = service.GetOrderById(short.Parse(str));
                if (data != null)
                {
                    txtId.Text = data.OrderId.ToString();
                    txtAddress.Text = data.Address;
                    txtFee.Text = data.Fee.Value.ToString();
                    txtPhone.Text = data.Phone.ToString();
                    ddlStatus.SelectedValue = data.Status.Value.ToString();
                    txtNote.Text = data.Note.Trim();
                    ddlCity.SelectedValue = data.City.ToString();
                    ddlCity_SelectedIndexChanged(null, null);
                    ddlDistrict.SelectedValue = data.District.ToString();
                    ddlDistrict_SelectedIndexChanged(null,null);
                    ddlWard.SelectedValue = data.Ward.ToString();
                }

            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            string str = Request["OrderId"];

            if (str == null)
            {
                
            }
            else
            {
                var data = service.GetOrderById(short.Parse(str));
                hdModified.Value = "true";
                int retcode = service.UpdateOrder(short.Parse(str), data.Name, txtAddress.Text, txtNote.Text.Trim(), txtPhone.Text,
                                                data.TransportType, float.Parse(txtFee.Text), (int)data.PayType, int.Parse(ddlStatus.SelectedValue),
                                                int.Parse(ddlCity.SelectedValue), int.Parse(ddlDistrict.SelectedValue), int.Parse(ddlWard.SelectedValue));
                lblStatus.Text = retcode == (int)Common.RetCode.SUCESS ? "Cập nhật thành công..." : "Cập nhật không thành công...";
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtFee.Text = string.Empty;
            txtAddress.Text = string.Empty;
            txtPhone.Text = string.Empty;
            ddlStatus.SelectedIndex = 0;
            txtNote.Text = string.Empty;
        }

        
    }
}
